package com.zyht.common.web.listener;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wx.commons.Global;
import com.wx.commons.Global.KEY;
import com.wx.core.vo.SysMenuSet;
import com.wx.entity.SysMenu;
import com.wx.service.SysMenuService;

public class WebConfigContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event){

	}

	@Override
	public void contextInitialized(ServletContextEvent event){

		ServletContext context = event.getServletContext();
		ApplicationContext ctx = WebApplicationContextUtils
			.getRequiredWebApplicationContext(context);

		SysMenuService sysMenuService = ctx.getBean(SysMenuService.class);
		List<SysMenu> list = sysMenuService.getMenuList();
		SysMenuSet ps = new SysMenuSet(list);
		ps.toSet();
		Global.CONFIG_MAP.put(KEY.SYSMENU,ps);
		setWebConfig(context);
		
		//设置基础路径
		setBasePath(context);
		try{
			ServerInfo.systeminfo();
			ServerInfo.networkinfo();
		} catch (Exception e){}
	}

	private void setWebConfig(ServletContext context){
		context.setAttribute("sysmenu",Global.CONFIG_MAP.getObject(KEY.SYSMENU));
		String systemProfile = context.getInitParameter(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME);
		if(null!=systemProfile && !"".equals(systemProfile)){
			//设置系统profile参数
			context.setAttribute(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME,systemProfile);
		}
	}
	
	private void setBasePath(ServletContext context){
		context.setAttribute("basePath", context.getContextPath());
	}
	
}
